package com.adopapa.nezhacloud.authc.service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.adopapa.nezhacloud.authc.domain.RolePerm;
import com.adopapa.nezhacloud.authc.domain.RolePermRepository;
import com.adopapa.nezhacloud.authc.domain.UserInfo;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.service.BaseDomainService;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

@Service
public class RolePermService extends BaseDomainService<RolePerm> {

	@Autowired
	private RolePermRepository rolePermRepository;

	public RolePerm getRolePermById(String id) {
		Optional<RolePerm> optional = rolePermRepository.findById(id);
		return optional.isPresent() ? optional.get() : null;
	}
	
	public List<RolePerm> getRolePermByUserInfo(UserInfo userInfo) {
		return rolePermRepository.findByUserInfo(userInfo);
	}

	public Page<RolePerm> getRolePermPage(int pageNo, int pageSize, RolePerm rolePerm) {
		PageRequest pageRequest = buildPageRequest(pageNo, pageSize, Order.desc("createTime"));
		Specification<RolePerm> specification = buildSpecification(rolePerm);
		Page<RolePerm> page = rolePermRepository.findAll(specification, pageRequest);
		return page;
	}

	public RolePerm saveRolePerm(RolePerm rolePerm) {
		return rolePermRepository.saveAndFlush(rolePerm);
	}

	public List<RolePerm> saveRolePerms(List<RolePerm> rolePerms) {
		return rolePermRepository.saveAllAndFlush(rolePerms);
	}

	public void deleteRolePerm(String id) {
		rolePermRepository.deleteById(id);
	}

	public void deleteRolePerm(RolePerm rolePerm) {
		rolePermRepository.delete(rolePerm);
	}

	public void deleteRolePerms(String... ids) {
		rolePermRepository.deleteAllByIdInBatch(Arrays.asList(ids));
	}

	public void deleteRolePerms(List<RolePerm> rolePerms) {
		rolePermRepository.deleteAllInBatch(rolePerms);
	}

	@SuppressWarnings("serial")
	@Override
	public Specification<RolePerm> buildSpecification(RolePerm rolePerm) {
		return new Specification<RolePerm>() {
			@Override
			public Predicate toPredicate(Root<RolePerm> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new LinkedList<>();
				if (!Tools.empty(rolePerm.getUserInfo()) //
						&& !Tools.empty(rolePerm.getUserInfo())) {
					predicates.add(cb.equal(root.join("userInfo").get("id"), //
							rolePerm.getUserInfo().getId()));
				}
				if (!Tools.empty(rolePerm.getRoleInfo())//
						&& !Tools.empty(rolePerm.getRoleInfo())) {
					predicates.add(cb.equal(root.join("roleInfo").get("id"), //
							rolePerm.getRoleInfo().getId()));
				}
				return cb.and(predicates.toArray(new Predicate[0]));
			}

		};
	}

	// predicates.add(cb.equal(cb.substring(root.get("datagram"), 1, 23),
	// dataPrefix));

}
